package com.deer.wms.system.manage.web;

import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.system.manage.model.permission.Permission;
import com.deer.wms.system.manage.model.resource.Resource;
import com.deer.wms.system.manage.model.resource.ResourceCriteria;
import com.deer.wms.system.manage.model.resource.ResourceDto;
import com.deer.wms.system.manage.model.resource.ResourceUrl;
import com.deer.wms.system.manage.service.PermissionService;
import com.deer.wms.system.manage.service.ResourceService;
import com.deer.wms.system.manage.service.ResourceUrlService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* Created by guo on 2017/10/08.
*/

@Api(description = "资源信息api接口")
@RestController
@RequestMapping("/resources")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private ResourceUrlService resourceUrlService;

    @PostMapping("/add")
    public Result add(@RequestBody ResourceDto resourceDto) {

        Resource resource = new Resource();
        resource.setResourceCode(resourceDto.getResourceCode());
        resource.setResourceName(resourceDto.getResourceName());
        resource.setResourceType(resourceDto.getResourceType());
        resource.setParentId(resourceDto.getParentId());
        resource.setResourceGroup(resourceDto.getResourceGroup());
        resource.setLevel(resourceDto.getLevel());
        resource.setSortNumber(resourceDto.getSortNumber());
        resource.setIcon(resourceDto.getIcon());
        String url = resourceDto.getUrl();
        if (url == null || "".equals(url)) {
            resourceService.save(resource);
        }
        else {
            resourceService.save(resource);

            ResourceUrl resourceUrl = new ResourceUrl();
            resourceUrl.setUrl(resourceDto.getUrl());
            resourceUrl.setResourceId(resource.getResourceId());
            resourceUrl.setUrlType(resourceDto.getUrlType());

            resourceUrlService.save(resourceUrl);
        }

        Permission permission = new Permission();
        permission.setObjectId(1);

        Integer resourceId = resource.getResourceId();

        permission.setResourceId(resourceId);
        permissionService.save(permission);

        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete(Integer resourceId) {
        /*Resource resource = new Resource();
        resource = resourceService.getById(resourceId);
        resource.setState("deleted");*/
        resourceService.removeById(resourceId);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody ResourceDto resource) {
        resourceService.updateById(resource);
        Integer resourceId = resource.getResourceId();
        ResourceUrl resourceUrl = resourceUrlService.findBy("resource_id",resourceId);
        if(resourceUrl !=null){
            resourceUrl.setUrl(resource.getUrl());
            resourceUrlService.updateById(resourceUrl);
        }else if(resource.getUrl() != null && !"".equals(resource.getUrl())){
              resourceUrl = new ResourceUrl();
            resourceUrl.setUrl(resource.getUrl());
            resourceUrl.setResourceId(resource.getResourceId());
            resourceUrl.setUrlType(resource.getUrlType());

            resourceUrlService.save(resourceUrl);
        }

        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        Resource resource = resourceService.getById(id);
        return ResultGenerator.genSuccessResult(resource);
    }

    @GetMapping("/list")
    public Result list(ResourceCriteria criteria) {
//        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<ResourceDto> list = resourceService.findList();
//        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(list);
    }

    @GetMapping("/listPage")
    public Result listPage(ResourceCriteria criteria) {
        List<ResourceDto> list = resourceService.findPageList();
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

}
